'use strict';

angular.module('app')
    .controller('accountRecDetailController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "应收账款详情";
            var id = $state.params.id;
            var iden = $state.params.iden;
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
            activate(id,iden);
            // 初始化页面
            function activate(id,iden) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/accountRec/read/detail',
                    data: {'id': id,'iden':iden}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
           //审核
            $scope.approve = function(id, state) {
                var remark = $('#remark').val();
                if( $scope.record.accountType=='6'&& state==5){
                    $('#backCashModal').modal('show', true);
                    return;
                }
                $scope.alertMessage({
                    text: "确定做此操作吗？", //内容
                    btnCancel: true, //取消按钮
                    btnConfirm: true //确认按钮
                }).then(function () {
                    $.ajax({
                        url: $scope.app.project_name + '/accountRec/update/approve',
                        data: {'id': id, 'state': state, 'remark': remark}
                    }).then(function (result) {
                        $scope.loading = false;
                        if (result.httpCode == 200) {
                            if(result==-2){
                                $scope.alertModal({'content': '该申请没有主账户，请确认后在审核'});
                                $state.go('main.finance.accountRec.list');
                            }else{
                            $scope.alertModal({'content': '操作成功'});
                            $state.go('main.finance.accountRec.list');
                            }
                        } else {
                            $scope.msg = result.msg;
                            $scope.alertModal({'content': '操作失败' + $scope.msg});
                        }
                        $scope.$apply();
                    });
                });
            }

            $scope.backTradApprove=function (id, state) {
                if(!$scope.Validate($("#backCashModalfrom"))){
                    return;
                }
                var backTradNO = $('#backTradNO').val();
                $.ajax({
                    url: $scope.app.project_name + '/accountRec/update/approve',
                    data: {'id': id, 'state': state, 'remark': backTradNO}
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        if(result==-2){
                            $scope.alertModal({'content': '该申请没有主账户，请确认后在审核'});
                            $state.go('main.finance.accountRec.list');
                        }else{
                            $scope.alertModal({'content': '操作成功'});
                            $state.go('main.finance.accountRec.list');
                        }
                    } else {
                        $scope.msg = result.msg;
                        $scope.alertModal({'content': '操作失败' + $scope.msg});
                    }
                    $scope.$apply();
                });
            }

            $scope.approveFail = function(id, state) {
                var remark = $('#remark').val();
                if(state=='3'){
                    if(remark==null||remark==""){
                        $scope.alertModal({'content':'请输入拒绝理由'});
                        return;
                    }
                }
                $.ajax({
                    url : $scope.app.project_name + '/accountRec/update/approve',
                    data: {'id': id, 'state' : state,'remark':remark}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode ==200) {
                        if(result==-2){
                            $scope.alertModal({'content': '该申请没有主账户，请确认后在审核'});
                            $state.go('main.finance.accountRec.list');
                        }else {
                            $scope.alertModal({'content': '操作成功'});
                            $state.go('main.finance.accountRec.list');
                        }
                    } else {
                        $scope.alertModal({'content':'操作失败:'+result.msg});
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.preview = function(){
                $scope.firstApply = true;
            }

            $scope.clikShowNo = function(){
                $('#refusePlatfrom').modal('show', true);
            }
} ]);